package cn.myword.wordTemple.mapper;

import cn.myword.wordTemple.util.MyWordTemplateEngineUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Setter;
import org.apache.velocity.VelocityContext;

import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class UniqueWordGenerate {

    private Map<String, Object> params;
    /**
     * -- SETTER --
     *  设置源模板word路径
     */
    @Setter
    protected String srcPath;
    private int num;

    public UniqueWordGenerate() {
        this.num = 0;
        this.params = new HashMap<>();
    }

    /**
     *
     * push 一级参数将对文档对应占位符直接替换，
     *
     * 例如{
     *  "demo":"示例"
     * }
     * 将会直接替换文档${demo}内容
     *
     * 表格循环将进行数据替换，替换按list{param1},list{param2},list{param3}...进行替换，表格将会根据内容自动填行数据
     * */
    protected void pushList(List<Map<String,Object>> list){
        params.put("list"+num, list);
        num++;
    }

    protected void pushParam(String paramName, Object paramValue){
        params.put(paramName, paramValue);
    }
    /**
     * 返回
     * ("file",file); 生成文件
     * ("path",targetPath); 本地保存路径
     *
     * */
    protected JSONObject generate() throws Exception {
        if(srcPath!=null&&!srcPath.equals("")){
        }else srcPath= Paths.get(System.getProperty("user.dir"),
                "model").toString()+"/temple.docx";
        return MyWordTemplateEngineUtil.generator(srcPath, new VelocityContext(params));
    }


}
